

import 'package:bookshelf/event/read_block_event.dart';

import '../event/inventory_event.dart';
import '../log/logger.dart';
import '../main.dart';
import '../util/analysis_book_rule.dart';
import '../util/convert_util.dart';

class ResultHandler{

  String result = "";
  bool isAnalyze = false;

  Set<String> set = {};

  dataReceived(hexString){

    result += hexString;

    print('result=$result');

    if(settingEntity.log??false){
      Logger().write('result = $result\r\n');
    }

    if(!result.startsWith('1B')){
      Logger().write('clearResult = $result\r\n');
      result = '';
    }

    //询查标签
    //1B000A00018609010002060000D9641B000A0001860901000207000083B8
    if(result.length >= 26 && result.substring(10,14) == "8609"){
      analyze();
      return;
    }

    //天线错误
    if(result.length >= 22 && result.substring(10,14) == "9609"){
      result = "";
      return;
    }
    //读块
    if(result.length >= 40 && result.substring(10,14) == "8607"){
      var re = result.substring(38,40);
      if(re == '38'){
        eventFactory.fire(ReadBlockEvent(null));
        result = '';
        return;
      }
      if(result.length >= 68){
        var block = result.substring(40,64);
        print('---$block');

        var no = analysisBookRule(block);
        eventFactory.fire(ReadBlockEvent(no));
        result = '';
      }
      return;
    }


  }

  //1b000800018609010001072200
  analyze(){
    var len = result.substring(4,6);
    var state = result.substring(16,18);
    var ant = result.substring(18,22);

    //无标签或超时
    if((state == '00' || state == '36') ){
      eventFactory.fire(InventoryEvent([],ant));
      if(result.length>=30){
        result = result.substring(30);
      }
      if(result.length == 26 && len == '08'){
        result = result.substring(26);
      }
      if(result.isNotEmpty){
        analyze();
      }
    }

    // //无标签或超时
    // if((state == '00' || state == '36') && result.length>=30){
    //   eventFactory.fire(InventoryEvent([],ant));
    //
    //   if(result.isNotEmpty){
    //     analyze();
    //   }
    // }

    //有标签
    if(state == '80'){
      var length = int.parse(result.substring(22,24),radix: 16);
      if(result.length == 26+length*18+4){
        analyzeUid(result,ant,length);
        result = result.substring(26+length*18+4);
        if(result.isNotEmpty){
          analyze();
        }
      }
    }

  }

  analyzeUid(String data,ant,length){

    data = data.substring(26,data.length-4);
    while(data.isNotEmpty){
      set.add(flip(data.substring(0,16)));
      data = data.substring(18);
    }
    var list = set.toList();
    eventFactory.fire(InventoryEvent(list,ant));
    set.clear();
  }

  analyzeBlock() {

    if(isAnalyze){
      return;
    }
    isAnalyze = true;
    if (result.length >= 24 && result.substring(12, 14) == "95") {
      result = '';
      isAnalyze = false;
      eventFactory.fire(ReadBlockEvent(null));
      return;
    }
    // String blockCount = result.substring(12, 14);
    // int count = hexToInt(blockCount);

    if (result.length >= 50) {
      var block = '';
      for(int i = 0; i<3;i++){
        int start = 16+(i+1)*2+i*8;
        block = block + result.substring(start,start+8);
      }
      var no = analysisBookRule(block);
      eventFactory.fire(ReadBlockEvent(no));

      result = result.substring(50);
      isAnalyze = false;
      if(result.isNotEmpty){
        analyzeBlock();
      }
    }else{
      isAnalyze = false;
    }
  }

  clear(){
    if(result.isNotEmpty){
      Logger().write('clearResult = $result\r\n');
    }
    result = '';
  }

}